package com.unicair.hancher.disableapp.list;

import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.unicair.hancher.disableapp.R;
import com.unicair.hancher.disableapp.mvp.MainPresenter;
import com.unicair.hancher.disableapp.utils.AppInfoBean;
import com.unicair.hancher.disableapp.utils.HancherLogUtil;

import java.util.ArrayList;

import butterknife.Unbinder;

/**
 * 作者：Hancher
 * 时间：2019/1/12.
 * 邮箱：ytu_shaoweijie@163.com
 * 版本：v1.0
 * <p>
 * 说明：
 */
public class MainAdapter extends RecyclerView.Adapter<MainViewHolder> implements View.OnClickListener {
    private Context mContext;
    private MainPresenter mPresenter;
    private PackageManager mPackageManager;
    private ArrayList<AppInfoBean> mList = new ArrayList<AppInfoBean>();
    private Unbinder binder;

    public MainAdapter(Context context, MainPresenter presenter, PackageManager packageManager) {
        this.mContext = context;
        this.mPresenter = presenter;
        this.mPackageManager = packageManager;
    }
    public void changeData(ArrayList<AppInfoBean> list){
        HancherLogUtil.d("list change, size="+list.size());
        mList = list;
    }
    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View convertView = LayoutInflater.from(mContext).inflate(R.layout.item_app_info_list, viewGroup, false);
        MainViewHolder viewHolder = new MainViewHolder(convertView);
        if (convertView.getTag() == null){
            convertView.setTag(viewHolder);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder viewHolder, int i) {
        viewHolder.title.setText(mList.get(i).getName());
        viewHolder.content.setText(mList.get(i).getPackageName());
        viewHolder.imageView.setImageDrawable(mList.get(i).getIcon());
        viewHolder.enable.setText("可用否："+mList.get(i).isEnabled());
        viewHolder.layout.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onClick(View v) {
        if (!(v.getTag() instanceof MainViewHolder)){
            HancherLogUtil.w("can not get view holder");
            return;
        }
        MainViewHolder viewHolder = (MainViewHolder) v.getTag();
        int position = viewHolder.getLayoutPosition();
        String appClassName = mList.get(position).getPackageName();
        boolean enable = mList.get(position).isEnabled();
        mPresenter.changetAppEnable(appClassName,!enable);
    }
}
